﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;

namespace LetiLib.Connection
{
    public class Receiver
    {
        public static bool bAcceptCmdHeader(byte[] btReceive)
        {
            if (btReceive.Length <= 0)
                return false;
            //check first byte
            if (btReceive[0] != (byte)TAG.DEVICE_TAG)
                return false;
            return true;
        }

        public static int bAcceptPackageHeader(byte[] btReceive)
        {
            int ret = -1;

            for (int i = 0; i < btReceive.Length; i++)
            {

                if (btReceive[i] == (byte)TAG.DEVICE_TAG)// && btReceive[i + 1] == DEFS.CMD_BLACKBOX_DATALOG)
                {
                    ret = i;
                    break;
                }
            }

            return ret;
        }
        public static bool bAcceptVersionCmd(byte[] btReceive, byte bClientType)
        {
            if (btReceive.Length != 5 || btReceive[0] != bClientType)
                return false;
            //check if btReceive[4] is an integer
            try
            {
                Int16.Parse(btReceive[4].ToString());
                return true;
            }
            catch
            {
                return false;
            }


        }

        // tungpt added >>
        public static bool bAcceptPlaylistReqCmd(byte[] btReceive)
        {
            if (btReceive[0] == (byte)TAG.DEVICE_TAG &&
                btReceive[1] == (byte)TAG.CMD_SERVER_PLAYLIST_UPDATE_REQ &&
                btReceive[2] == (byte)TAG.RES_PLAYLIST_UPDATE_REQUEST_OK)
                return true;
            else return false;
        }
        public static bool bAcceptPlaylistInfo(byte[] btReceive)
        {
            if (btReceive[0] == (byte)TAG.DEVICE_TAG &&
                btReceive[1] == (byte)TAG.PLAYLIST_INFO_HEADER &&
                btReceive[2] == (byte)TAG.RES_PLAYLIST_INFO_RECEIVED)
                return true;
            else return false;
        }

        public static bool bAcceptDataReqCmd(byte[] btReceive)
        {
            if (btReceive[0] == (byte)TAG.DEVICE_TAG &&
                btReceive[1] == (byte)TAG.CMD_SERVER_DATA_UPDATE_REQ &&
                btReceive[2] == (byte)TAG.RES_DATA_UPDATE_REQUEST_OK)
                return true;
            else return false;
        }
        public static bool bAcceptDataInfo(byte[] btReceive)
        {
            if (btReceive[0] == (byte)TAG.DEVICE_TAG &&
                btReceive[1] == (byte)TAG.DATA_INFO_HEADER &&
                btReceive[2] == (byte)TAG.RES_DATA_INFO_RECEIVED)
                return true;
            else return false;
        }


        public static bool bAcceptDataPackage(byte[] btReceive)
        {
            if (btReceive[0] == (byte)TAG.DEVICE_TAG &&
                btReceive[1] == (byte)TAG.DATA_PACKAGE_HEADER &&
                btReceive[2] == (byte)TAG.RES_DATA_PACKAGE_RECEIVED)
                return true;
            else return false;
        }
        public static bool bAcceptDataPackageIDError(byte[] btReceive)
        {
            if (btReceive[0] == (byte)TAG.DEVICE_TAG &&
                btReceive[1] == (byte)TAG.DATA_PACKAGE_HEADER &&
                btReceive[2] == (byte)TAG.RES_DATA_PACKAGE_ERROR_PACKAGEID)
                return true;
            else return false;
        }
        public static bool bAcceptDataPackageError(byte[] btReceive)
        {
            if (btReceive[0] == (byte)TAG.DEVICE_TAG &&
                btReceive[1] == (byte)TAG.DATA_PACKAGE_HEADER &&
                btReceive[2] == (byte)TAG.RES_DATA_PACKAGE_ERROR_CHECKSUM)
                return true;
            else return false;
        }

        public static bool bAcceptFirmwareReqCmd(byte[] btReceive)
        {
            if (btReceive[0] == (byte)TAG.DEVICE_TAG &&
                btReceive[1] == (byte)TAG.CMD_SERVER_FIRMWARE_UPDATE_REQ &&
                btReceive[2] == (byte)TAG.RES_FIRMWARE_UPDATE_REQUEST_OK)
                return true;
            else return false;
        }
        public static bool bAcceptFirmwareInfo(byte[] btReceive)
        {
            if (btReceive[0] == (byte)TAG.DEVICE_TAG &&
                btReceive[1] == (byte)TAG.FIRMWARE_INFO_HEADER &&
                btReceive[2] == (byte)TAG.RES_FIRMWARE_INFO_RECEIVED)
                return true;
            else return false;
        }


        public static bool bAcceptFirmwarePackage(byte[] btReceive)
        {
            if (btReceive[0] == (byte)TAG.DEVICE_TAG &&
                btReceive[1] == (byte)TAG.FIRMWARE_PACKAGE_HEADER &&
                btReceive[2] == (byte)TAG.RES_FIRMWARE_PACKAGE_RECEIVED)
                return true;
            else return false;
        }
        public static bool bAcceptFirmwarePackageIDError(byte[] btReceive)
        {
            if (btReceive[0] == (byte)TAG.DEVICE_TAG &&
                btReceive[1] == (byte)TAG.FIRMWARE_PACKAGE_HEADER &&
                btReceive[2] == (byte)TAG.RES_FIRMWARE_PACKAGE_ERROR_PACKAGEID)
                return true;
            else return false;
        }
        public static bool bAcceptFirmwarePackageError(byte[] btReceive)
        {
            if (btReceive[0] == (byte)TAG.DEVICE_TAG &&
                btReceive[1] == (byte)TAG.FIRMWARE_PACKAGE_HEADER &&
                btReceive[2] == (byte)TAG.RES_FIRMWARE_PACKAGE_ERROR_CHECKSUM)
                return true;
            else return false;
        }
        // << ended

        //CMDs sent by GPS modules
        public static bool bAcceptIMEI(byte[] btReceive)
        {
            if (btReceive[0] == (byte)TAG.DEVICE_TAG && btReceive[1] == (byte)TAG.CMD_IMEI_REQUEST && btReceive.Length == 17)
            {
                return true;
            }
            else return false;
        }

        public static bool bAcceptWarningTooHot(byte[] btReceive)
        {
            if (btReceive[0] == (byte)TAG.DEVICE_TAG && btReceive[1] == (byte)TAG.WARNING_TOO_HOT && btReceive.Length == 2)
            {
                return true;
            }
            else return false;
        }

        public static bool bAcceptWarningDamaged(byte[] btReceive)
        {
            if (btReceive[0] == (byte)TAG.DEVICE_TAG && btReceive[1] == (byte)TAG.WARNING_DAMAGED && btReceive.Length == 2)
            {
                return true;
            }
            else return false;
        }

        public static bool bAcceptWarningPowerLost(byte[] btReceive)
        {
            if (btReceive[0] == (byte)TAG.DEVICE_TAG && btReceive[1] == (byte)TAG.WARNING_OUT_OF_POWER && btReceive.Length == 2)
            {
                return true;
            }
            else return false;
        }

        public static bool bAcceptWarningOpenned(byte[] btReceive)
        {
            if (btReceive[0] == (byte)TAG.DEVICE_TAG && btReceive[1] == (byte)TAG.WARNING_OPENNED && btReceive.Length == 2)
            {
                return true;
            }
            else return false;
        }

        public static bool bAcceptGetActivePageResponse(byte[] btReceive)
        {
            if (btReceive[0] == (byte)TAG.DEVICE_TAG && btReceive[1] == (byte)TAG.CMD_GET_ACTIVE_PAGE && btReceive.Length == 4)
            {
                return true;
            }
            else return false;
        }

        public static bool bAcceptDatalog(byte[] btReceive)
        {
            if (btReceive[0] == (byte)TAG.DEVICE_TAG && btReceive[1] == (byte)TAG.CMD_BLACKBOX_DATALOG && btReceive.Length > 4)
                return true;
            else return false;
        }
    }
}

